//
//  LBCCategoryHomeHeaderV.m
//  喜马拉雅改良版框架
//
//  Created by dllo on 15/12/30.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LBCCategoryHomeHeaderV.h"

@interface LBCCategoryHomeHeaderV ()<UIScrollViewDelegate>

@property (nonatomic, strong)UIPageControl *page;
@property (nonatomic, strong)NSMutableArray *imageArr;

@end

@implementation LBCCategoryHomeHeaderV


-(instancetype)initWithFrame:(CGRect)frame array:(NSMutableArray *)array{
    self = [super initWithFrame:frame];
    if (self) {
        _imageArr = [NSMutableArray array];

        _imageScrollV = [UIScrollView new];
        _imageScrollV.delegate = self;
        _imageScrollV.pagingEnabled = YES;
        _imageScrollV.showsHorizontalScrollIndicator = NO;
        [self addSubview:_imageScrollV];
        [_imageScrollV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.left.equalTo(self);
            
        }];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        for (LBCCategoryImageModel *imageM in array) {
            [tempArr addObject:imageM.pic];
            [_imageArr addObject:imageM.pic];
        }
        [_imageArr insertObject:[tempArr lastObject] atIndex:0];
        [_imageArr addObject:[tempArr firstObject]];
        
        _page = [UIPageControl new];
        [self addSubview:_page];
        [_page mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(@-10);
            make.left.mas_equalTo(SCREEN_WIDTH / 4);
            make.right.mas_equalTo(-SCREEN_WIDTH / 4);
            make.height.mas_equalTo(@20);
            
        }];
        //设置页码个数
        _page.numberOfPages = array.count;
        //设置当前页码颜色
        _page.currentPageIndicatorTintColor = [UIColor whiteColor];
        //设置未选中页码颜色
        _page.pageIndicatorTintColor = [UIColor grayColor];
        //设置当前选中页码
        _page.currentPage = 0;
        
        UIView *subV = [UIView new];
        [_imageScrollV addSubview:subV];
        [subV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_imageScrollV);
            make.height.equalTo(_imageScrollV);
            
        }];
        
        UIImageView *lastImage = nil;
        for (int i = 0; i < _imageArr.count; i++) {
            UIImageView *focusImages = [UIImageView new];
            focusImages.userInteractionEnabled = YES;
            focusImages.tag = 1000+ i;
            [subV addSubview:focusImages];
            [focusImages sd_setImageWithURL:[NSURL URLWithString:_imageArr[i]] placeholderImage:[UIImage imageNamed:ImagePlaceHolder]];
            [focusImages mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.width.equalTo(_imageScrollV);
                if (lastImage) {
                    make.left.equalTo(lastImage.mas_right);
                } else {
                    make.left.equalTo(_imageScrollV);
                }
                
            }];
            lastImage = focusImages;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            [focusImages addGestureRecognizer:tap];
        }
        [subV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(lastImage.mas_right);
            
        }];
    
        [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(changePhoto:) userInfo:@"time" repeats:YES];
        

    }
    return self;
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    UIImageView *imageView = (UIImageView *)tap.view;
    [self.categoryHomeHeaderDelegate presentToMusicPlayertagtrackId:imageView.tag - 1001];
}

#pragma mark- 减速停止时触发
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (_imageScrollV.contentOffset.x == SCREEN_WIDTH * (_imageArr.count - 1)) {
        _imageScrollV.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    } else if (_imageScrollV.contentOffset.x == 0) {
        _imageScrollV.contentOffset = CGPointMake(SCREEN_WIDTH * (_imageArr.count - 2), 0);
    }
    
    _page.currentPage = (_imageScrollV.contentOffset.x - SCREEN_WIDTH) / SCREEN_WIDTH;
    
}
#pragma mark- 定时器方法实现
-(void)changePhoto:(NSTimer *)time{
    // 记录开始的偏移量
    [UIView animateWithDuration:0.5 animations:^{
        
        CGPoint tempContentSize = _imageScrollV.contentOffset;
        tempContentSize.x += SCREEN_WIDTH;
        _imageScrollV.contentOffset = tempContentSize;
        
        UIScrollView *tempSV = [UIScrollView new];
        tempSV.contentOffset = _imageScrollV.contentOffset;
    }];
    if(_imageScrollV.contentOffset.x == (_imageArr.count - 1) * SCREEN_WIDTH){
        _imageScrollV.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    } else if(_imageScrollV.contentOffset.x == 0){
        _imageScrollV.contentOffset = CGPointMake(SCREEN_WIDTH * (_imageArr.count - 2), 0);
    }
    _page.currentPage  = (_imageScrollV.contentOffset.x - SCREEN_WIDTH) / SCREEN_WIDTH ;
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
